package cn.edu.sspu.www.controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sspu.www.domain.BaseTeacher;
import cn.edu.sspu.www.service.BaseTeacherService;

/**
 * 教师 信息控制器
 * @author warm
 * @version 2019年07月11日
 */
@Controller
@RequestMapping("/teacher")
public class BaseTeacherController {
	
	@Autowired
	private BaseTeacherService service;

	/**
	 * 列表
	 * @param baseTeacher
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, BaseTeacher queryVo) {
		
		List<BaseTeacher> list = service.queryBaseTeacherList(queryVo);
		
		ModelAndView view = new ModelAndView();
		view.addObject("list", list);
		view.addObject("queryVo", queryVo);
		view.setViewName("teacher/list");
		return view;
	}
	
	/**
	 * 新增操作
	 * @param baseTeacher
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(HttpServletRequest request, BaseTeacher baseTeacher) {
		
		int count = service.addBaseTeacher(baseTeacher);
		
		ModelAndView view = new ModelAndView();
		if (count == 1) {
			view.setViewName("redirect:/teacher/list.action");
		} else {
			view.addObject("msg", "新增失败！");
			view.setViewName("teacher/edit");
		}
		return view;
	}
	
	/**
	 * 新增页面
	 * @param baseTeacher
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView add(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		view.setViewName("teacher/edit");
		return view;
	}
	
	/**
	 * 修改操作
	 * @param baseTeacher
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView edit(HttpServletRequest request, BaseTeacher baseTeacher) {
		
		int count = service.editBaseTeacher(baseTeacher);
		
		ModelAndView view = new ModelAndView();
		if (count == 1) {
			view.setViewName("redirect:/teacher/list.action");
		} else {
			view.addObject("msg", "修改失败！");
			view.setViewName("teacher/edit");
		}
		return view;
	}
	
	/**
	 * 修改页面
	 * @param baseTeacher
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(HttpServletRequest request, Long id) {
		
		BaseTeacher baseTeacher = service.viewBaseTeacher(id);
		
		ModelAndView view = new ModelAndView();
		view.addObject("baseTeacher", baseTeacher);
		view.setViewName("teacher/edit");
		return view;
	}
	

	/**
	 * 详情页面
	 * @param baseTeacher
	 * @return
	 */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public ModelAndView view(HttpServletRequest request, Long id) {
		
		BaseTeacher baseTeacher = service.viewBaseTeacher(id);
		
		ModelAndView view = new ModelAndView();
		view.addObject("baseTeacher", baseTeacher);
		view.setViewName("teacher/view");
		return view;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(HttpServletRequest request, Long id) {
		
		int count = service.delBaseTeacher(id);
		
		ModelAndView view = new ModelAndView();
		if (count != 1) {
			view.addObject("msg", "删除失败！");
		}
		view.setViewName("redirect:/teacher/list.action");
		return view;
	}
}
